package manegement;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import dao.DBFacade;

import bean.CommentBean;
import bean.MediaBean;
import bean.MediaType;

public class MediaManager {

	private List<MediaBean> pictures;

	private List<MediaBean> sounds;

	private List<MediaBean> movies;

	public MediaManager(String id) {
		pictures = new ArrayList<MediaBean>();
		sounds = new ArrayList<MediaBean>();
		movies = new ArrayList<MediaBean>();
		for (Iterator<MediaBean> iterator = DBFacade.getInstance().getMediasByPost().iterator(); iterator.hasNext();) {
			MediaBean media = iterator.next();
			switch(media.getType()){
			case SOUND:
				sounds.add(media);
				break;
			case PICTURE:
				pictures.add(media);
				break;
			case MOVIE:
				movies.add(media);
				break;
			}
		}		

	}

	/**
	 * Attaches a sound
	 * @param description
	 * 			- Sound's description
	 * @param data
	 * 			- Data's sound
	 * @return soundId
	 *          - Identifier of the sound
	 * @throws Exception
	 */
	public String attachSound(String owner, String description, String data) throws Exception {
		if(data == null || data.equals(""))
			throw new Exception("Invalid Data");
		String id = getNewMediaId();
		sounds.add(new MediaBean(id, data, description,MediaType.SOUND, owner));
		return id;

	}

	/**
	 * Attaches a movie
	 * @param sessionID
	 * 			- Identifier of the session to validate the User logon
	 * @param postID
	 * 			- Identifier of the post that will be inserted sound
	 * @param description
	 * 			- Movie's description
	 * @param data
	 * 			- Data's movie
	 * @return moviedId
	 *          - Identifier of the movie
	 * @throws Exception
	 */
	public String attachMovie(String owner,	String description, String data) throws Exception {
		if(data == null || data.equals(""))
			throw new Exception("Invalid Data");
		String id = getNewMediaId();
		movies.add(new MediaBean(id, data, description,MediaType.MOVIE, owner));
		return id;
	}

	/**
	 * Attaches a picture
	 * @param sessionID
	 * 			- Identifier of the session to validate the User logon
	 * @param postID
	 * 			- Identifier of the post that will be inserted picture
	 * @param description
	 * 			- Picture's description
	 * @param data
	 * 			- Data's picture
	 * @return pictureId
	 *          - Identifier of the picture
	 * @throws Exception
	 */
	public String attachPicture(String owner,String description, String data) throws Exception {
		if(data == null || data.equals(""))
			throw new Exception("Invalid Data");
		String id = getNewMediaId();
		pictures.add(new MediaBean(id, data, description,MediaType.PICTURE, owner));
		return id;

	}

	/**
	 * Returns the number of sounds from post
	 * @param postId
	 * 			- Identifier of the post sounds to be counted
	 * @return number of sounds
	 * @throws Exception
	 */
	public int getNumberOfSounds() {
		return sounds.size();
	}

	/**
	 * Returns the number of pictures from post
	 * @param postId
	 * 			- Identifier of the post pictures to be counted
	 * @return number of pictures
	 * @throws Exception
	 */
	public int getNumberOfPictures() {
		return pictures.size();
	}

	/**
	 * Returns the number of movies from post
	 * @param postId
	 * 			- Identifier of the post movies to be counted
	 * @return number of movies
	 * @throws Exception
	 */
	public int getNumberOfMovies() {
		return movies.size();

	}

	/**
	 * Capturing the sound of the post with the index passed as parameter
	 * @param postId
	 * 			- Identifier of the post which will capture the sound
	 * @param index
	 * 			- Sound's index
	 * @return soundId
	 * @throws Exception
	 */
	public String getSound(int index) throws Exception {
		if(index < 0 || index > sounds.size())
			throw new Exception("Invalid index");

		return sounds.get(index).getId();
	}

	/**
	 * Capturing the movie of the post with the index passed as parameter
	 * @param postId
	 * 			- Identifier of the post which will capture the movie
	 * @param index
	 * 			- Movie's index
	 * @return movieId
	 * @throws Exception
	 */
	public String getMovie(int index) throws Exception {
		if(index < 0 || index > movies.size())
			throw new Exception("Invalid index");

		return movies.get(index).getId();	
	}

	/**
	 * Capturing the picture of the post with the index passed as parameter
	 * @param postId
	 * 			- Identifier of the post which will capture the picture
	 * @param index
	 * 			- Picture's index
	 * @return pictureId
	 * @throws Exception
	 */
	public String getPicture(int index) throws Exception {
		if(index < 0 || index > pictures.size())
			throw new Exception("Invalid index");

		return pictures.get(index).getId();
	}


	/**
	 * Returns data of sound
	 * @param soundId
	 * @return data of sound
	 * @throws Exception 
	 */
	public String getMovieData(String mediaId) throws Exception{
		return 	getMediaData(mediaId, movies);
	}

	/**
	 * Returns data of sound
	 * @param soundId
	 * @return data of sound
	 * @throws Exception 
	 */
	public String getPictureData(String mediaId) throws Exception{
		return 	getMediaData(mediaId, pictures);
	}

	/**
	 * Returns data of sound
	 * @param soundId
	 * @return data of sound
	 * @throws Exception 
	 */
	public String getSoundData(String mediaId) throws Exception{
		return 	getMediaData(mediaId, sounds);
	}

	/**
	 * Returns description of sound
	 * @param soundId
	 * @return description of sound
	 * @throws Exception 
	 */
	public String getSoundDescription(String mediaId) throws Exception{
		return getMediaDescription(mediaId, sounds);
	}

	/**
	 * Returns description of sound
	 * @param soundId
	 * @return description of sound
	 * @throws Exception 
	 */
	public String getMovieDescription(String mediaId) throws Exception{
		return getMediaDescription(mediaId, movies);
	}

	/**
	 * Returns description of sound
	 * @param soundId
	 * @return description of sound
	 * @throws Exception 
	 */
	public String getPictureDescription(String mediaId) throws Exception{
		return getMediaDescription(mediaId, pictures);
	}


	/**
	 * Delete a movie
	 * @param sessionId
	 * 			- Identifier of the session to validate the User logon
	 * @param movieId
	 * 			- Identifier of the movie will be deleted
	 */
	public void deleteMovie(String movieId) {
		deleteMedia(movieId, movies);
	}

	/**
	 * Delete a sound
	 * @param sessionId
	 * 			- Identifier of the session to validate the User logon
	 * @param soundId
	 * 			- Identifier of the sound will be deleted
	 */
	public void deleteSound(String soundId) {
		deleteMedia(soundId, sounds);

	}

	/**
	 * Delete a picture
	 * @param sessionId
	 * 			- Identifier of the session to validate the User logon
	 * @param pictureId
	 * 			- Identifier of the picture will be deleted
	 */
	public void deletePicture(String pictureId) {
		deleteMedia(pictureId, pictures);
	}

	public void save(){
		List<MediaBean> media = new ArrayList<MediaBean>();
		media.addAll(pictures);
		media.addAll(movies);
		media.addAll(sounds);
		DBFacade.getInstance().saveMedias(media);
	}

	private String getNewMediaId(){
		return String.valueOf(DBFacade.getInstance().getLastMediaId()); 
	}


	private String getMediaData(String mediaId, List<MediaBean> medias) throws Exception{
		for(Iterator<MediaBean> it = medias.iterator(); it.hasNext();){
			MediaBean media = it.next();
			if(media.getId().equals(mediaId))
				return media.getURL();
		}
		throw new Exception("Invalid id");
	}

	private String getMediaDescription(String mediaId, List<MediaBean> medias) throws Exception{
		for(Iterator<MediaBean> it = medias.iterator(); it.hasNext();){
			MediaBean media = it.next();
			if(media.getId().equals(mediaId))
				return media.getDescription();
		}
		throw new Exception("Invalid id");
	}

	private void deleteMedia(String mediaId, List<MediaBean> medias) {
		MediaBean media, chooseMedia = null;

		for(Iterator<MediaBean> it = medias.iterator(); it.hasNext();){
			media = it.next();
			if(media.getId().equals(mediaId))
				chooseMedia = media;
		}
		if(chooseMedia != null)
			medias.remove(chooseMedia);

	}

}
